<template>
  <!-- 对话框 -->
  <el-dialog
    :title="form.id?'编辑部门':'新增部门'"
    :visible="visible"
    width="50%"
    @close="onDialog"
  >

    <el-form ref="form" :rules="adsDeptFormRules" :model="form" label-width="100px">
      <el-form-item prop="name" label="部门名称">
        <el-input v-model.trim="form.name" placeholder="长度为1-10位" />
      </el-form-item>
      <el-form-item prop="code" label="部门编码">
        <el-input v-model.trim="form.code" placeholder="长度为1-10位" />
      </el-form-item>
      <el-form-item prop="manager" label="部门负责人">
        <!-- <el-input v-model.trim="form.manager" placeholder="长度为1-10位" /> -->
        <el-select v-model="form.manager" placeholder="请选择" @focus="getDeptsManager">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="introduce" label="部门介绍">
        <el-input v-model.trim="form.introduce" placeholder="长度为1-50位" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="$emit('input',false)">取 消</el-button>
      <el-button type="primary" @click="OnSave">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { getCompanyInfo, addDepartments, getDepartmentsById, editDepartmentsById } from '@/api/departments.js'
import { getEmployeesListSimple } from '@/api/employees.js'
export default {
  model: {
    prop: 'visible'
  },
  props: {
    visible: {
      type: Boolean,
      required: true
    },
    node: {
      type: Object,
      required: true
    }
  },
  data() {
    const ValidatorName = async(rule, value, callback) => {
      let flag = false
      let flag2 = false
      if (this.form.id) {
        // 编辑
        const res = await getCompanyInfo()
        const newArr = res.depts.filter(item => item.pid === this.node.pid && item.id !== this.node.id)
        flag = newArr.some(item => item.name === value)
      } else {
        // 新增
        if (!this.node.children) return callback()
        flag = this.node.children.some(item => item.name === value)
        flag2 = (this.node.name === value)
      }
      if (flag || flag2) {
        return callback(new Error(value + '部门名称已存在'))
      } else {
        callback()
      }
      // if (value === '总裁办') {
      //   callback(new Error('部门名称不能为总裁办'))
      // }
      // callback()
    }
    const ValidatorCode = async(rule, value, callback) => {
      const res = await getCompanyInfo()
      let flag = false
      if (this.form.id) {
        // 编辑
        const newArr = res.depts.filter(item => item.code !== this.node.code)
        flag = newArr.some(item => item.code === value)
      } else {
        // 添加
        flag = res.depts.some(item => item.code === value)
      }
      if (flag) return callback(new Error('该部门编码已存在'))
      callback()
    }
    return {
      form: {
        name: '',
        manager: '',
        introduce: '',
        code: ''
      },
      adsDeptFormRules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 1, max: 10, message: '部门名称长度为1-10位', trigger: 'blur' },
          { validator: ValidatorName, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请输入部门负责人名称', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 1, max: 50, message: '部门介绍长度为1-50位', trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 1, max: 10, message: '部门编码长度为1-10位', trigger: 'blur' },
          { validator: ValidatorCode, trigger: 'blur' }
        ]
      },
      options: []
    }
  },

  created() {

  },

  methods: {
    // 关闭对话框
    onDialog() {
      this.$emit('input', false)
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        manager: '',
        introduce: '',
        code: ''
      }
    },
    async getDeptsManager() {
      const res = await getEmployeesListSimple()
      this.options = res
    },
    async OnSave() {
      try {
        await this.$refs.form.validate()
        if (this.form.id) {
        // 编辑
          await editDepartmentsById(this.form)
          this.$message.success('修改成功')
          this.$emit('edit-success')
        } else {
        // 新增
          this.form.pid = this.node.id
          await addDepartments(this.form)
          this.$message.success('修改成功')
          this.$emit('on-success')
        }
        this.onDialog()
      } catch (err) {
        console.log(err)
      }
    },
    // 获取最新数据
    async getDeptsInfo(id) {
      console.log('点击了编辑', id)
      const res = await getDepartmentsById(id)
      this.form = res
    }
  }
}
</script>

<style scoped lang='less'>

</style>
